package com.spice.service.util;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.stereotype.Component;

import javax.security.auth.Subject;
import java.io.Serializable;

/**
 * @author yhm
 */
@Component
public class ShiroUtil {

    public static Serializable getSessionId(){
        return SecurityUtils.getSubject().getSession().getId();
    }


    public static boolean checkPermission(String... Permission) {
        try {
            SecurityUtils.getSubject().checkPermissions(Permission);
            return true;
        } catch (AuthorizationException e) {
            return false;
        }
    }

    public static boolean isAuthenticated() {
        return SecurityUtils.getSubject().isAuthenticated();
    }

    public static boolean hasRoles(String... roles) {
        for (String role :
                roles) {
            if (!SecurityUtils.getSubject().hasRole(role)) {
                return false;
            }
        }
        return true;
    }
}